#!/bin/bash
set -e
#
if ! [ $(id -u) = 0 ]; then
   echo "This script must be run as root."
   exit 1
fi
#
if [ ! $FS_VERSION ]; then
   echo "This script must be run by 'make install' in '/usr2/fs', aborting."
   exit
fi
echo "********************************************************************"
echo "*  This is the INSTALLATION script for Field System version        *"
echo "*  $FS_VERSION *"
echo "********************************************************************"
echo "CAUTION: This script will automatically modify some of your directories."
echo "On /usr2 each of the following directories will be created and filled"
echo "  with their default contents (unless they already exist): oper, prog,"
echo "  control, sched, proc, log, and st. The permissions and ownerships of all"
echo "  the files will be set (including for /etc/gpib.conf)."
echo "If the /usr2/oper directory is created, any existing /home/oper directory"
echo "  will be renamed to /home/oper.FSCOPY (unless it exists already), any"
echo "  existing /usr2/oper will be renamed to /usr2/oper.FSCOPY (unless it"
echo "  exists already), and if /home is not a symbolic link and /home/oper"
echo "  already existed, /home/oper will be made a symbolic link to /usr2/oper."
echo "  N.B. If /home is a link (it must be a symbolic link to /usr2)."
echo "A parallel operation will occur for /usr2/prog".
echo "The link /usr2/fs will be deleted and made to point to this version of"
echo "  the Field System. A similar link structure will be used for /usr2/st."
echo "This is a fairly benign operation on most systems and always safe on a"
echo "  standard system. To get the default versions of any already existing"
echo "  directories, you will need to delete them before continuing with this"
echo "  script."
echo "** This script must be excuted by root to work correctly. ** "
echo -n "Do you wish to continue with the INSTALLATION? (y=yes, n=no) : " 
badans=true
while [ "$badans" = "true" ]
do
  read ans
  case "$ans" in
    y|yes) echo "O.K. Continuing with install ... "
           badans=false
           ;;
    n|no)  echo "O.K. Exiting."
           exit
           ;;
    *)     echo -n "Please answer with y=yes or n=no : "
  esac
done
#
echo "Creating /usr2/fs directory link ..."
cd /usr2
if [ -L fs ]; then
  rm -rf fs
fi
if [ -e fs ]; then
  echo "/usr2/fs exists and is not a just link. I can't continue without"
  echo "possibly destroying your data. Please move or remove /usr2/fs."
  exit
fi
ln -s $FS_DIRECTORY fs
#
echo "Setting /usr2/fs permissions ..."
cd /usr2
chown -R prog $FS_DIRECTORY
chgrp -R rtx $FS_DIRECTORY
chmod -R a+r,u+w,go-w $FS_DIRECTORY
#
if [ ! -e st-0.0.0 -a ! -e st ]; then
  echo "Creating /usr2/st default directory structure..."
  mkdir /usr2/st-0.0.0
  cd /usr2/fs/st.default/st-0.0.0
  find Makefile misc -print|cpio -pmdu /usr2/st-0.0.0
  cd /usr2/st-0.0.0
  mkdir help
  mkdir bin
  cd /usr2
  rm -f st
  ln -s st-0.0.0 st
  chown -R prog st-0.0.0
  chgrp -R rtx st-0.0.0
  chmod -R a+r,u+w,go-w st-0.0.0
fi
#
# the following works for both /home and /usr2 separate OR /home -> /usr2
# and for the home directory originally on /home or /usr2
#
if [ ! -e /usr2/oper/bin ]; then
   echo "Setting up /usr2/oper ..."
   if [ -e /home/oper.FSCOPY -o -L /home/oper.FSCOPY ]; then
      if [ -e /home/oper -o -L /home/oper ]; then
         echo "/home/oper.FSCOPY already exists. Please move or remove it."
         exit
      fi
   fi
   if [ -e /usr2/oper.FSCOPY -o -L /usr2/oper.FSCOPY ]; then
      if [ -e /usr2/oper -o -L /usr2/oper ]; then
         echo "/usr2/oper.FSCOPY already exists. Please move or remove it."
         exit
      fi
   fi
#
   cd /home
   if [ -e oper -o -L oper ]; then
      mv oper oper.FSCOPY
      if [ ! -L /home ]; then
         cd /home
         ln -s /usr2/oper oper
      fi
   fi
#
# in case /usr2 is distinct
   cd /usr2
   if [ -e oper -o -L oper ]; then
      mv oper oper.FSCOPY
   fi
#
   cd /usr2/fs/st.default
   find oper -print|cpio -pmdu /usr2
   chown -R oper /usr2/oper
   chgrp -R rtx  /usr2/oper
   chmod -R a+r,u+w,go-w /usr2/oper
#
fi
#
# the following works for both /home and /usr2 separate OR /home -> /usr2
# and for the home directory originally on /home or /usr2
#
if [ ! -e /usr2/prog/bin ]; then
   echo "Setting up /usr2/prog ..."
   if [ -e /home/prog.FSCOPY -o -L /home/prog.FSCOPY ]; then
      if [ -e /home/prog -o -L /home/prog ]; then
         echo "/home/prog.FSCOPY already exists. Please move or remove it."
         exit
      fi
   fi
   if [ -e /usr2/prog.FSCOPY -o -L /usr2/prog.FSCOPY ]; then
      if [ -e /usr2/prog -o -L /usr2/prog ]; then
         echo "/usr2/prog.FSCOPY already exists. Please move or remove it."
         exit
      fi
   fi
#
   cd /home
   if [ -e prog -o -L prog ]; then
      mv prog prog.FSCOPY
      if [ ! -L /home ]; then
         cd /home
         ln -s /usr2/prog prog
      fi
   fi
#
# in case /usr2 is distinct
   cd /usr2
   if [ -e prog -o -L prog ]; then
      mv prog prog.FSCOPY
   fi
#
   cd /usr2/fs/st.default
   find prog -print|cpio -pmdu /usr2
   chown -R prog /usr2/prog
   chgrp -R rtx  /usr2/prog
   chmod -R a+r,u+w,go-w /usr2/prog
#
fi
#
if [ ! -e /usr2/control ]; then
   echo "Setting up /usr2/control ..."
   cd /usr2/fs/st.default
   find control -print|cpio -pmdu /usr2
   chown -R oper /usr2/control
   chgrp -R rtx  /usr2/control
   chmod -R a+r,ug+w,o-w /usr2/control
fi
#
if [ ! -e /usr2/proc ]; then
   echo "Setting up /usr2/proc ..."
   cd /usr2/fs/st.default
   find proc/station.prc -print|cpio -pmdu /usr2
   chown -R oper /usr2/proc
   chgrp -R rtx  /usr2/proc
   chmod -R a+r,ug+w,o-w /usr2/proc
fi
#
if [ ! -e /usr2/sched ]; then
   echo "Setting up /usr2/sched ..."
   mkdir /usr2/sched
   chown -R oper /usr2/sched
   chgrp -R rtx  /usr2/sched
   chmod -R a+r,ug+w,o-w /usr2/sched
fi
#
if [ ! -e /usr2/log ]; then
   echo "Setting up /usr2/log ..."
   mkdir /usr2/log
   chown -R oper /usr2/log
   chgrp -R rtx  /usr2/log
   chmod -R a+r,ug+w,o-w /usr2/log
fi
#
if [ -e /etc/gpib.conf ]; then
   echo "Setting permissions for /etc/gpib.conf ..."
   cd /etc
   chown root         gpib.conf
   chgrp rtx          gpib.conf
   chmod a+r,ug+w,o-w gpib.conf
fi
#
if [ ! -e /usr2/tle_files ]; then
   echo "Setting up /usr2/tle_files ..."
   cd /usr2/fs/st.default
   find tle_files/station.qth -print|cpio -pmdu /usr2
   chown -R oper /usr2/tle_files
   chgrp -R rtx  /usr2/tle_files
   chmod -R a+r,ug+w,o-w /usr2/tle_files
fi
#
echo "Done."
